package LinkerLoader;

/**
 * This class contains some of the functions shared by other classes in this package.<br>
 * @author Aashish Kumar
 */
public class Shared {
    
    /**
     * This function returns a sub array.Only index from begin_index to end_index will be picked.<p>
     * Modified Log: None.<br>
     * Error Condition tested: None.<br>
     * Creation Date : May 27,2008.<br>
     * Original Author: Aashish Kumar.<br>
     * @param array any string array.
     * @param begin_index the starting location 
     * @param end_index the ending location.
     * @return a new string array which is subarray of the input string array.
     */
    public String[] subArray(String[] array, int begin_index, int end_index){
        String[] mod_array = new String[end_index-begin_index+1];
        for(int index=0; index<mod_array.length; index++){
            mod_array[index] = array[begin_index+index];
        }
        return mod_array;
    }
    
    /**
     * This function throw errors to screen only.<p>
     * Modified Log: None.<br>
     * Error Condition tested: None.<br>
     * Creation Date : May 27,2008.<br>
     * Original Author: Aashish Kumar.<br>
     * @param error_code Error code.
     * @param text file name due to which the error is caused.
     */
    public void throwError(int error_code, String text, String[] file_address, int file_index, int index){
        if(Loader.linker_debugging_value.contentEquals("OFF")){//command line execution.
            if(error_code==190){
                System.out.println("Severe Error (190) in line "+index+" of text records in file "+file_address[file_index]+" : not defined as SHVAR in another program." +
                                   " Label replaced by zero.");
                System.out.println("Corrective Action : All Exvar Should be defined as Shvar in some program.");
            }else if(error_code==191){
                System.out.println("Severe Error 191 in line  "+index+" of text records in file "+file_address[file_index]+" : operands out of range. Exvar replaced by zero.");
                System.out.println("Corrective Action : Resultant operand should lie in between 0 to 1023.");
            }
        }else{
            if(error_code==190){
                System.out.println("Severe Error 190 : "+text+"not defined as SHVAR in another program." +
                                   " Label replaced by zero.");
                System.out.println("Corrective Action : All Exvar Should be defined as Shvar in some program.");
            }else if(error_code==191){
                System.out.println("Severe Error 191 : operands out of range. Exvar replaced by zero.");
                System.out.println("Corrective Action : Resultant operand should lie in between 0 to 1023.");
            }
        }
    }

    /**
     * This function throw errors to screen only.<p>
     * Modified Log: None.<br>
     * Error Condition tested: None.<br>
     * Creation Date : May 27,2008.<br>
     * Original Author: Aashish Kumar.<br>
     * @param error_code Error code.
     * @param text file name due to which the error is caused.
     */
    public void throwError(int error_code, String text){
        if(error_code==193){
            System.out.println("Severe Error 193: Memory out of range in linking a file. "+text+" File ignored.");
            System.out.println("Suggested Fix : Total memory used by all the linking files should be maximum 1024.");
        }else if(error_code==192){
            System.out.println("Severe Error 192: Memory out of range in linking. "+text+" Linking halted.");
            System.out.println("Suggested Fix : Linking Program should use a maximum of 1024 memory words.");
        }else if(error_code==190){
            System.out.println("Severe Error 190 : "+text+"not defined as SHVAR in another program." +
                               " Label replaced by zero.");
            System.out.println("Corrective Action : All Exvar Should be defined as Shvar in some program.");
        }else if(error_code==191){
            System.out.println("Severe Error 191 : operands out of range. Exvar replaced by zero.");
            System.out.println("Corrective Action : Resultant operand should lie in between 0 to 1023.");
        }
    }
    
    /**
     * This function calculates the sum of the number of Linking records in the files input to the linker.<p>
     * Modified Log: None.<br>
     * Error Condition tested: None.<br>
     * Creation Date : May 27,2008.<br>
     * Original Author: Aashish Kumar.<br>
     * @param file_address a String[] containing the address of each of the linker's input file.
     * @return the length of the load map.
     */
    public int getLinkerTextRecordLength(String[] file_address){
        Linker linker_instance = new Linker();
        int linker_text_record_length=0;
        for(int index=0; index<file_address.length; index++){
            linker_text_record_length+=linker_instance.readFile(file_address[index],"T").length;
        }
        return linker_text_record_length;
    }
}